package com.atguigu.lister.limitation;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * Created by 小可乐$ on 2024/9/11$ 19:58$
 * description: 消费者限流，需要yaml中配置prefetch：参数，一次取回多少个：        prefetch: 1
 */
@Component
@Slf4j
public class RabbitMQLimitationLister {

    @RabbitListener(queues = "atguigu.queue.direct")
    public void processMessage(String messageContent, Message message, Channel channel) throws IOException, InterruptedException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        log.info("消费者限流test"+messageContent);
        // System.out.println(10 / 0);
        TimeUnit.SECONDS.sleep(1);
//        basicAck:给Broker返回ACK确认信息，表示消息已经在消费端成功消费，这样Broker就可以把消息删除了
      channel.basicAck(deliveryTag,false);
    }

}
